package com.res.share.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.res.share.model.ResDown;
import com.res.share.mapper.ResDownMapper;
import com.res.share.model.ResUserScore;
import com.res.share.service.IResDownService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.res.share.service.IResStatisticsService;
import com.res.share.service.IResUserScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 下载 服务实现类
 * </p>
 *
 * @author astupidcoder
 * @since 2021-09-21
 */
@Service
public class ResDownServiceImpl extends ServiceImpl<ResDownMapper, ResDown> implements IResDownService {


    @Autowired
    private IResStatisticsService resStatisticsService;

    @Autowired
    private IResUserScoreService resUserScoreService;

    @Override
    public IPage<ResDown> findResDownList(Page<ResDown> page, Integer userId) {
        return baseMapper.findResDownList(page, userId);
    }

    @Override
    public void saveResDown(Integer userId, Integer articleId, Integer freeType) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("ART_ID",articleId);
        queryWrapper.eq("CREATE_USER_ID",userId);
        queryWrapper.eq("IS_DEL",0);
        ResDown resDown = baseMapper.selectOne(queryWrapper);
        if(resDown!=null){
            baseMapper.updateResDownNum(userId,articleId);
        }else {
            resDown=new ResDown();
            resDown.setArtId(articleId);
            resDown.setCreateUserId(userId);
            resDown.setNum(1);
            baseMapper.insert(resDown);
            if(freeType==1){
                ResUserScore resUserScore=new ResUserScore();
                resUserScore.setUserId(userId);
                resUserScore.setArtId(articleId);
                resUserScore.setResFrom("下载");
                resUserScore.setType(1);
                resUserScore.setStatus(0);
                resUserScore.setScore(3);
                resUserScore.setCreateUserId(userId);
                int resScoreData = resUserScoreService.saveResScoreData(resUserScore);
                if(resScoreData>0){
                    resStatisticsService.updateUserScoreNum(userId,3);
                }
            }
        }
    }

    @Override
    public Integer findResDownNums(Integer articleId) {
        return baseMapper.findResDownNums(articleId);
    }

    @Override
    public boolean checkArticleUpload(Integer userId, Integer articleId) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("ART_ID",articleId);
        queryWrapper.eq("CREATE_USER_ID",userId);
        queryWrapper.eq("IS_DEL",0);
        ResDown resDown = baseMapper.selectOne(queryWrapper);
        if(resDown!=null){
            return  true;
        }
        return false;
    }
}
